// pages/games/battleResult/battleResult.js
var app = getApp();
var config = require('../../../config.js');
var masterTotalScore;
var slaveTotalScore;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    try {
      var roomID = wx.getStorageSync(config.roomId);
      console.log("当前房间的rooId是" + roomID);
      if (roomID) {
        this.setData({
          roomId: roomID
        });
      }
    } catch (e) {
    }

    if (options.pageId == config.battleResult) {
      //请求服务器 获取对战结果
      wx.request({
        url: config.getBattleResult,
        data: {
          rn: that.data.roomId
        },
        success: function (res) {
          if (res.data.success == 0) {
            console.log(res.data.rows);
            var resObj = res.data.rows;
            if (resObj){
              masterTotalScore = resObj.masterTotalScore;
              slaveTotalScore = resObj.slaveTotalScore;
              that.setStatusV2(res.data.rows);
            }
           
          }
        },

        fail: function (error) {
          console.log(error)
        }
      })      



    } else {
      masterTotalScore = options.masterTotalScore;
      slaveTotalScore = options.slaveTotalScore;
      console.log("masterTotalScore = " + masterTotalScore + "slaveTotalScore= " + slaveTotalScore);
      this.setUserInfo();
      try {
        var resObj = wx.getStorageSync(config.currRoomInfo)
        this.setStatus(resObj);
      } catch (e) {
        // Do something when catch error
      }
    }
  },


  //设置用户信息
  setUserInfo: function () {
    var that = this;
    this.setData({
      userFaceUrl: app.globalData.userInfo.avatarUrl,
      nickName: app.globalData.userInfo.nickName,
    });
  },

  /**
   * 设置游戏比分 输赢状态
   */
  setStatus: function (resObj) {
    var that = this;
    if (resObj) {
      // Do something with return value
      if (resObj.masterOpenId == app.globalData.openid) {
        //我是房主 设置受邀者信息
        if (resObj.slaveUser) {
          that.setData({
            slaveUserFace: resObj.slaveUser.avatarUrl,
            slaveUserNickName: resObj.slaveUser.nickname,
            userScore: masterTotalScore,
            otherScore: slaveTotalScore
          });
          //判断输赢
          var winValue;
          if (masterTotalScore > slaveTotalScore) {
            winValue = 1;

          } else if (masterTotalScore == slaveTotalScore) {
            winValue = 2;
            //平局
          } else {
            //输了
            winValue = 3;
          }

          that.setData({
            winValue: winValue
          });

        }

      } else {
        //如果是受邀者  设置房主信息
        console.log("房主头像地址==" + resObj.masterUser.avatarUrl);
        that.setData({
          slaveUserFace: resObj.masterUser.avatarUrl,
          slaveUserNickName: resObj.masterUser.nickname,
          userScore: slaveTotalScore,
          otherScore: masterTotalScore
        });
        var winValue;
        //判断输赢
        if (slaveTotalScore > masterTotalScore) {
          winValue = 1;
        } else if (slaveTotalScore == masterTotalScore) {
          //平局
          winValue = 2;
        } else {
          //输了
          winValue = 3;
        }
        that.setData({
          winValue: winValue
        });

      }

    }


  },


  /**
 * 设置游戏比分 输赢状态 V2  分享进入的
 */
  setStatusV2: function (resObj) {
    var that = this;
    if (resObj) {
      // Do something with return value
      //我是房主 设置受邀者信息
      if (resObj.slaveUser && resObj.masterUser) {
        that.setData({
          userFaceUrl: resObj.masterUser.avatarUrl,
          nickName: resObj.masterUser.nickname,
          slaveUserFace: resObj.slaveUser.avatarUrl,
          slaveUserNickName: resObj.slaveUser.nickname,
          userScore: masterTotalScore,
          otherScore: slaveTotalScore
        });
        //判断输赢
        var winValue;
        if (masterTotalScore > slaveTotalScore) {
          winValue = 1;

        } else if (masterTotalScore == slaveTotalScore) {
          winValue = 2;
          //平局
        } else {
          //输了
          winValue = 3;
        }

        that.setData({
          winValue: winValue
        });



      }

    }


  },


  /**
   * 继续挑战
   */
  goOn: function () {
    wx.navigateTo({
      url: '../../games/competition/competition',
    })


  },

  /**
   * 返回擂台
   */
  toIndex: function () {
    wx.redirectTo({
      url: '../../games/competition/competition',
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '我在（法加擂台赛）挑战，没有对手，欢迎来战！',
      path: '/pages/index/index?pageId=' + config.battleResult + "&roomId=" + that.data.roomId,
      success: function (res) {

      }
    }
  }

})